package net.qhzw.geologr5.gis.service.system.file2linux;


import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 远程解压缩指定目录下的指定名字的文件
 */
@Slf4j
public class ExtractUtils {

    /*
     * @Description:远程解压缩指定目录下的指定名字的文件
     * @param path:指定解压文件的目录
     * @param fileName:需要解压的文件名字
     * @param decpath :解压完成后的存放路径
     */
    public static Map<String, Object> remoteZipToFile(String path, String fileName, String decpath) {
        Map<String, Object> resultMap = new HashMap<>();
        List<String> dirList = new ArrayList<>();
        Boolean result = false;
        try {
            Connection connection = new Connection(SFTPInfo.SFTP_REQ_HOST);// 创建一个连接实例
            connection.connect();// Now connect
            boolean isAuthenticated = connection.authenticateWithPassword(SFTPInfo.SFTP_REQ_USERNAME,
                    SFTPInfo.SFTP_REQ_PASSWORD);// Authenticate
            if (isAuthenticated == false)
                throw new IOException("user and password error");
            Session sess = connection.openSession();// Create a session
            sess.requestPTY("bash");
            sess.startShell();
            InputStream stdout = new StreamGobbler(sess.getStdout());
            InputStream stderr = new StreamGobbler(sess.getStderr());
            BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
            BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));
            PrintWriter out = new PrintWriter(sess.getStdin());
            out.println("cd /" + path + "/");
            out.println("ls");
            log.info("fileName: {}", fileName);
            if (fileName.endsWith(".zip")) {
                out.println("unzip -O GBK -j " + fileName + "  -d /" + decpath + "/");//解压zip格式
            }
            if (fileName.endsWith(".gz")) {
                out.println("tar zxvf " + fileName + "  -C /" + decpath + "/");//解压tar格式
            }
            out.println("ls");
            out.println("exit");
            out.close();
            sess.waitForCondition(ChannelCondition.CLOSED | ChannelCondition.EOF | ChannelCondition.EXIT_STATUS, 30000);
            log.info("下面是从stdout输出");
            while (true) {
                String line = stdoutReader.readLine();
                if (line == null)
                    break;
                log.info("line1: {}", line);
                dirList.add(line);

            }
            log.info("下面是从stderr输出");
            while (true) {
                String line = stderrReader.readLine();
                if (line == null)
                    break;
                log.info("line2: {}", line);
                dirList.add(line);
            }
            log.info("ExitCode: {}", sess.getExitStatus());
            sess.close();/* Close this session */
            connection.close();/* Close the connection */
            result = true;
        } catch (IOException e) {
            e.printStackTrace(System.err);
            result = false;
            System.exit(2);
        }
        resultMap.put("status", result);
        resultMap.put("dirList", dirList);
        return resultMap;
    }

}
